/*
* 图片浏览
* */

import 'dart:convert';
// import 'package:cached_network_image/cached_network_image.dart';
import 'package:center_beijing/widgets/photo_view_gallery.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:images_picker/images_picker.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'dart:io';
import 'imageSelect_widget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class ImagesWatchWidget2 extends StatefulWidget {
  final List<String> photos;
  final bool ifEdit;
  final Function onchange;
  ImagesWatchWidget2({
    @required this.photos,
    this.ifEdit = false,
    this.onchange,
  });
  @override
  _ImagesWatchWidget2State createState() => _ImagesWatchWidget2State();
}

class _ImagesWatchWidget2State extends State<ImagesWatchWidget2> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  bool isDisplayingDetail = true;

  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  List<Widget> getWidgetList() {
    return widget.photos
        .asMap()
        .keys
        .map((index) => getItemContainer(index))
        .toList();
  }

  Widget getItemContainer(index) {
    return Padding(
      padding: const EdgeInsets.symmetric(
        horizontal: 5.0,
        vertical: 5.0,
      ),
      child: AspectRatio(
        aspectRatio: 1.0,
        child: Stack(
          children: <Widget>[
            Positioned.fill(child: _selectedAssetWidget(index)),
            widget.ifEdit?AnimatedPositioned(
              duration: kThemeAnimationDuration,
              top: isDisplayingDetail ? 2.0 : -30.0,
              right: isDisplayingDetail ? 2.0 : -30.0,
              child: _selectedAssetDeleteButton(index),
            ):Container()
          ],
        ),
      ),
    );
  }
  Widget _selectedAssetDeleteButton(int index) {
    return GestureDetector(
      onTap: () => removeAsset(index),
      child: DecoratedBox(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4.0),
          color: Colors.grey,
        ),
        child: Icon(
          Icons.close,
          color: Colors.white,
          size: 18.0,
        ),
      ),
    );
  }
  void removeAsset(int index) {
    setState(() {
      widget.photos.removeAt(index);
      widget.onchange(index);
    });
  }
  Widget _selectedAssetWidget(int index) {
    final String asset = widget.photos.elementAt(index);
    return GestureDetector(
      onTap: isDisplayingDetail
          ? () async {
        if(asset.split('.').last=='mp4'){
          Navigator.pushNamed(context, 'vedio_preview_page',
              arguments: asset+'&&'+'2');
        }else{
          Navigator.of(context).push(new FadeRoute(
              page: PhotoViewGalleryScreen(
                  images: widget.photos, //传入图片list
                  index: index, //传入当前点击的图片的index
                  heroTag: '')));
        }
      }
          : null,
      child: RepaintBoundary(
        child: ClipRRect(
          borderRadius: BorderRadius.circular(8.0),
          child: _assetWidgetBuilder(asset),
        ),
      ),
    );
  }

  Widget _assetWidgetBuilder(String asset) {
    Widget widget;
    widget = _imageAssetWidget(asset);
    return widget;
  }

  Widget _imageAssetWidget(String asset) {
    return Image.network(
      asset,
      height: 80.h,
      width: 80.w,
      fit: BoxFit.fill,
    );

  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
      child: GridView.count(
        physics:
        const NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        //水平子Widget之间间距
        crossAxisSpacing: 0.0,
        //垂直子Widget之间间距
        mainAxisSpacing: 0.0,
        //GridView内边距
        padding: EdgeInsets.all(1.0),
        //一行的Widget数量
        crossAxisCount: 4,
        //子Widget宽高比例
        childAspectRatio: 1.2,
        //子Widget列表
        children: this.getWidgetList(),
      ),
    );
  }
}
